package easy;

public class Solution_643 {

    public double findMaxAverage(int[] nums, int k) {
        int sum = 0;
        for (int i = 0; i < k; i++) {
            sum += nums[i];
        }
        int maxSum = sum;
        for (int i = k; i < nums.length; i++) {
            if (nums[i] == nums[i - k]) {
                continue;
            }
            sum += (nums[i] - nums[i - k]);
            if (nums[i] > nums[i - k]) {
                maxSum = Math.max(maxSum, sum);
            }
        }
        return (maxSum + 0.0) / k;
    }

    /*public double findMaxAverage(int[] nums, int k) {
        int a = 0, mod = 0;
        for (int i = 0; i < k; i++) {
            mod += nums[i];
            a += mod / k;
            mod %= k;
        }
        double avg = a + (mod + 0.0) / k;
        for (int i = k; i < nums.length; i++) {
            if (nums[i] == nums[i - k]) {
                continue;
            }
            mod = mod + nums[i] - nums[i - k];
            a += mod / k;
            mod %= k;
            if (nums[i] > nums[i - k]) {
                avg = Math.max(a + (mod + 0.0) / k, avg);
            }
        }
        return avg;
    }*/

    public static void main(String[] args) {
        Solution_643 model = new Solution_643();

        System.out.println(model.findMaxAverage(new int[]{1,12,-5,-6,50,3}, 4));
        System.out.println(model.findMaxAverage(new int[]{0, 4, 0, 3, 2}, 1));
//        System.out.println(-7 / 4);
//        System.out.println(-7 % 4);
    }
}
